package com.api.regression.group.searchgroups;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.api.test.AbstractApiTest;
import com.zyncro.api.IGroupApiService;
import com.zyncro.api.exceptions.ZyncroApiException;
import com.zyncro.api.model.GroupApi;
import com.zyncro.api.model.PagedItemsResultApi;

@ContextConfiguration(locations = { "classpath:/group/group-context.xml" })
public class SearchGroups extends AbstractApiTest {

	private static final long DEFAULT_TIME_DIFFERENCE = 100;

	@Value("#{groupsearch['user.searcher'].appId}")
	private String userUrn;

	@Value("#{groupsearch['user.searcher'].password}")
	private String userPassword;

	@Value("#{groupsearch[search].sharedText}")
	private String sharedText;

	@Value("#{groupsearch[search].notSharedText}")
	private String notSharedText;

	@Value("#{groupsearch['user.groupCreator'].appId}")
	private String userFilterUrn;

	@Value("#{groupsearch['group.dateCreation'].urn}")
	private String dateCreationGroupUrn;

	@Value("#{groupsearch[search].cantShared}")
	private int cantSharedGroups;

	@Value("#{groupsearch[search].cantNotShared}")
	private int cantNotSharedGroups;

	@Value("#{groupsearch[search].cantUserShared}")
	private int cantUserSharedGroups;

	private long fromDate;

	private long toDate;

	@BeforeClass
	public void loginUser() throws ZyncroApiException {

		getApi().login(userUrn, userPassword);

		GroupApi group = getApi().getService(IGroupApiService.class).getGroup(dateCreationGroupUrn).getObject();

		this.fromDate = group.getCreationTime() - DEFAULT_TIME_DIFFERENCE;
		this.toDate = group.getCreationTime() + DEFAULT_TIME_DIFFERENCE;
	}

	@Test
	public void testSearchSharedText() throws ZyncroApiException {

		int pageNumber = 0;
		int itemsPerPage = 0;
		List<String> users = null;
		String fromDate = null;
		String toDate = null;
		boolean onlyResultSize = false;

		PagedItemsResultApi<GroupApi> result = getApi().getService(IGroupApiService.class).searchGroups(pageNumber, itemsPerPage, sharedText,
				users, fromDate, toDate, onlyResultSize);

		Assert.assertEquals(result.getTotalItems(), cantSharedGroups, "Total groups sharing text");
	}

	@Test
	public void testSearchNotSharedText() throws ZyncroApiException {

		int pageNumber = 0;
		int itemsPerPage = 0;
		List<String> users = null;
		String fromDate = null;
		String toDate = null;
		boolean onlyResultSize = false;

		PagedItemsResultApi<GroupApi> result = getApi().getService(IGroupApiService.class).searchGroups(pageNumber, itemsPerPage, notSharedText,
				users, fromDate, toDate, onlyResultSize);

		Assert.assertEquals(result.getTotalItems(), cantNotSharedGroups, "Total groups not sharing text");
	}

	@Test
	public void testSearchSharedTextFilteringUser() throws ZyncroApiException {

		int pageNumber = 0;
		int itemsPerPage = 0;
		List<String> users = new ArrayList<String>();
		users.add(userFilterUrn);

		String fromDate = null;
		String toDate = null;
		boolean onlyResultSize = false;

		PagedItemsResultApi<GroupApi> result = getApi().getService(IGroupApiService.class).searchGroups(pageNumber, itemsPerPage, sharedText,
				users, fromDate, toDate, onlyResultSize);

		Assert.assertEquals(result.getTotalItems(), cantUserSharedGroups, "Total groups sharing text and user");
	}

	@Test
	public void testSearchSharedTextFilteringDate() throws ZyncroApiException {

		int pageNumber = 0;
		int itemsPerPage = 0;
		List<String> users = null;
		String fromDate = String.valueOf(this.fromDate);
		String toDate = String.valueOf(this.toDate);
		boolean onlyResultSize = false;

		PagedItemsResultApi<GroupApi> result = getApi().getService(IGroupApiService.class).searchGroups(pageNumber, itemsPerPage, sharedText,
				users, fromDate, toDate, onlyResultSize);

		Assert.assertEquals(result.getTotalItems(), 1, "Total groups sharing text and date");
	}

	@Test
	public void testSearchSharedTextOnlySize() throws ZyncroApiException {

		int pageNumber = 0;
		int itemsPerPage = 0;
		List<String> users = null;
		String fromDate = null;
		String toDate = null;
		boolean onlyResultSize = true;

		PagedItemsResultApi<GroupApi> result = getApi().getService(IGroupApiService.class).searchGroups(pageNumber, itemsPerPage, sharedText,
				users, fromDate, toDate, onlyResultSize);

		Assert.assertEquals(result.getTotalItems(), cantSharedGroups, "Only size search");
		Assert.assertEquals(result.getElements().size(), 0);
	}
}
